package ahxu.crawler;

import java.net.URI;
import java.net.URISyntaxException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class DefaultCrawlerSiteDao implements ICrawlerSiteDao {
	private static final Log logger = LogFactory.getLog(DefaultCrawlerResultDao.class);

	private DataSource dataSource;
	public static final String table="CRAWL_SITE";
	
	public DefaultCrawlerSiteDao(DataSource dataSource){
		this.dataSource=dataSource;
	}
	
	public List<CrawlerSite> findResults() throws CrawlerSiteDaoException {
		try{
			List<CrawlerSite> ret = new ArrayList<CrawlerSite>();
			String sql = "select * from " + table;

			SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(
					dataSource);

			ParameterizedRowMapper<CrawlerSite> mapper = new ParameterizedRowMapper<CrawlerSite>() {

				// notice the return type with respect to Java 5 covariant
				// return types
				public CrawlerSite mapRow(ResultSet rs, int rowNum)
						throws SQLException {
					CrawlerSite obj = new CrawlerSite();
					obj.setId(rs.getString("id"));
					UriInput input=new UriInput();
					input.setAnchorText(rs.getString("SITE_NAME"));
					try {
						input.setUri(new URI(rs.getString("URL")));
					} catch (URISyntaxException e) {
						e.printStackTrace();
					}
					obj.setInput(input);
					obj.setDeep(rs.getInt("DEEP"));
					obj.setTotal(rs.getInt("TOTAL"));
					obj.setLimitSameSite(rs.getBoolean("SAME_SITE"));
					return obj;
				}
			};
			ret = simpleJdbcTemplate.query(sql, mapper);

			return ret;
		}catch(Exception e){
			throw new CrawlerSiteDaoException(e);
		}
	}

	public String save(CrawlerSite object) throws CrawlerSiteDaoException {
		try{
			if(this.dataSource!=null){
				String insertSQL="insert into " +table+
				"(ID,SITE_NAME,URL,TOTAL,SAME_SITE, DEEP)" +
				"values" +
				"(?,?,?,?,?,?)";
				SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);

				simpleJdbcTemplate.update(insertSQL,
						object.getId(),
						object.getInput().getAnchorText(),
						object.getInput().getUri(),
						object.getTotal(),
						object.isLimitSameSite(),
						object.getDeep());
			}
			logger.info("save content to db");
			return object.getId();
		}catch(Exception e){
			logger.error(e.getMessage());
			throw new CrawlerSiteDaoException(e);
		}
	}

	public void delete(String id) throws CrawlerSiteDaoException {
		try{
			if(this.dataSource!=null){
				String insertSQL="delete from " +table+
				" where id=?";
				SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);

				simpleJdbcTemplate.update(insertSQL,id);
			}
			logger.info("save content to db");
			
		}catch(Exception e){
			logger.error(e.getMessage());
			throw new CrawlerSiteDaoException(e);
		}
		
	}

	public CrawlerSite findCrawlerSite(String id) throws CrawlerSiteDaoException {
		try{
			CrawlerSite ret =null;
			String sql = "select * from " + table+" where id=?";

			SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(
					dataSource);

			ParameterizedRowMapper<CrawlerSite> mapper = new ParameterizedRowMapper<CrawlerSite>() {

				// notice the return type with respect to Java 5 covariant
				// return types
				public CrawlerSite mapRow(ResultSet rs, int rowNum)
						throws SQLException {
					CrawlerSite obj = new CrawlerSite();
					obj.setId(rs.getString("id"));
					UriInput input=new UriInput();
					input.setAnchorText(rs.getString("SITE_NAME"));
					try {
						input.setUri(new URI(rs.getString("URL")));
					} catch (URISyntaxException e) {
						e.printStackTrace();
					}
					obj.setInput(input);
					obj.setDeep(rs.getInt("DEEP"));
					obj.setTotal(rs.getInt("TOTAL"));
					obj.setLimitSameSite(rs.getBoolean("SAME_SITE"));
					return obj;
				}
			};
			ret = simpleJdbcTemplate.queryForObject(sql, mapper,id);

			return ret;
		}catch(Exception e){
			throw new CrawlerSiteDaoException(e);
		}
	}

}
